package com.wuming.caiwu.controller;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.CommonResult;
import com.wuming.caiwu.core.JsonResult;
import com.wuming.caiwu.model.Member;
import com.wuming.caiwu.model.QsgTeachingAssistant;
import com.wuming.caiwu.security.SecurityHander;
import com.wuming.caiwu.service.MemberService;
import com.wuming.caiwu.service.QsgTeachingAssistantService;
import com.wuming.caiwu.util.EncodeUitl;
import com.wuming.caiwu.util.OssUtil;
import com.wuming.caiwu.util.SHAUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by hanfei on 16/4/19.
 */
@Controller
@RequestMapping("/web/teachassistant")
public class TeachingAssistantController extends BaseController {

    @Autowired
    private QsgTeachingAssistantService teachingAssistantService;

    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult saveTeachingAssistant(QsgTeachingAssistant assistant) {
        try {
            List<QsgTeachingAssistant> teachingAssistants = teachingAssistantService.getTeachingAssistantByPhone(assistant.getPhone());
            if (null == teachingAssistants || teachingAssistants.isEmpty()) {
                teachingAssistantService.saveQsgTeachingAssistant(assistant);
                return new JsonResult<Object>(null, "注册用户成功", true);
            }else{
                teachingAssistantService.updateQsgTeachingAssistant(assistant);
                return new JsonResult<Object>(null, "更新用户成功.", true);
            }
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
        }
        return new JsonResult<Object>(null, "注册失败,请联系系统管理员.", false);
    }

    @RequestMapping(value = "manageTeachingAssistant", method = RequestMethod.GET)
    public String manageTeachingAssistant() {
        return "/teachassistant/manageTeachingAssistant";
    }

    @RequestMapping(value = "toAddAssistant", method = RequestMethod.GET)
    public String toAddAssistant() {
        return "/teachassistant/editTeachingAssistant";
    }


    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(defaultValue = "0") int pageIndex, @RequestParam(defaultValue = "10") int pageSize, @RequestParam(required = false) String nickname, @RequestParam(required = false) String teachLevel, @RequestParam(required = false) String phone) {
        try {
            List<QsgTeachingAssistant> teachingAssistants = teachingAssistantService.getTeachingAssistantByPage(nickname,teachLevel,pageIndex,pageSize);
            int total = teachingAssistantService.getTotal(nickname,teachLevel);
            return CommonResult.createSuccessResult(teachingAssistants, total, "查询用户信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createSuccessResult("查询用户信息失败");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createSuccessResult("查询用户信息失败");
        }
    }


    @ResponseBody
    @RequestMapping(value = "/saveAssistantImg", method = RequestMethod.POST)
    public JsonResult saveAssistantImg(MultipartFile file, Long assistantId) {
        QsgTeachingAssistant assistant = teachingAssistantService.getTeachingAssistantById(assistantId);
        String imageUrl = "";
        if(null != assistant){
            imageUrl = OssUtil.upload(file, OssUtil.FileDirType.ARTICLE_IMAGES);
            assistant.setImgUrl(imageUrl);
            teachingAssistantService.updateQsgTeachingAssistant(assistant);
        }else{
            imageUrl = OssUtil.upload(file, OssUtil.FileDirType.ARTICLE_IMAGES);
        }
        return new JsonResult<Object>(imageUrl, "上传助教图片信息成功.", true);
    }

    @RequestMapping(value = "/editAssistant/{id}", method = RequestMethod.GET)
    public String editTeachingAssistant(@PathVariable Long id, Model model) {
        logger.info("准备修改助教信息ID:{}", id);
        QsgTeachingAssistant assistant = teachingAssistantService.getTeachingAssistantById(id);
        model.addAttribute("assistant", assistant);
        return "/teachassistant/editTeachingAssistant";
    }
//
//    @ResponseBody
//    @RequestMapping(value = "/edit", method = RequestMethod.POST)
//    public CommonResult editMember(Member member) {
//        try {
//            memberService.modifyMember(member);
//            Member current = memberService.getMemberById(member.getId());
//            return CommonResult.createSuccessResult("保存注册用户成功.");
//        } catch (BusinessException be) {
//            logger.error("发生业务异常,异常信息:{}", be.getMessage());
//            return CommonResult.createFailureResult("保存注册用户失败.");
//        } catch (Exception e) {
//            logger.error("发生系统异常,异常信息:{}", e.getMessage());
//            return CommonResult.createFailureResult("保存注册用户失败.");
//        }
//    }
//
    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult deteleTeachingAssistant(@PathVariable Long id) {
        logger.info("准备助教信息ID:{}", id);
        try {
            teachingAssistantService.removeTeachingAssistantById(id);
            return CommonResult.createSuccessResult("删除助教成功");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("删除助教失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("删除助教失败.");
        }
    }
}
